package modes

import (
	"fmt"
	"time"
)

type TelRecord struct {
	Id     int64  `json:"tel_id" xorm:"id"`         //手机记录id
	UcId   int64  `json:"tel_ucid" xorm:"uc_id"`    //uid
	Phone  string `json:"tel_phone" xorm:"phone"`   //源手机号
	Moblie string `json:"tel_moblie" xorm:"moblie"` //现手机号
	At     int64  `json:"tel_at" xorm:"at"`         //插入时间/修改时间
}

func (this *TelRecord) TableName() string {
	return "tel_record"
}
func (this *TelRecord) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(0).Insert(this)
}

func (this *TelRecord) Get() (bool, error) {
	return Db(0).Get(this)
}
func (this *TelRecord) delete(where string) (int64, error) {
	return Db(0).Where(where).Delete(this)
}
func (this *TelRecord) Del() (int64, error) {
	where := fmt.Sprintf("id =%d", this.Id)
	return this.delete(where)
}
func (this *TelRecord) Count(where string) (int64, error) {
	var val TelRecord
	return Db(0).Where(where).Count(&val)
}
func (this *TelRecord) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *TelRecord) IdSet(field string) (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return this.update(where, field)
}

func (this *TelRecord) where(fage, count, page int, where, field string) ([]TelRecord, error) {
	list := make([]TelRecord, 0)
	var err error
	if field == "" {
		field = "id"
	}
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}
